using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using OpenComposite.Base.Designer;
using OpenComposite.DesignerControls;
using OpenComposite.EII.Designer.Generic;
using OpenComposite.EII.Forms;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Repository;
using OpenComposite.HumanActivity.Contracts.Participants;

namespace OpenComposite.EII.Designer.BRules.ParticipantRules
{
	public class THENParticipantSymbol : THENResultSymbol
	{
		#region Initialization
		public THENParticipantSymbol()
			: base()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			_participants = new Participants();
			this.MinimumSize = new Size(100, 50);
			txtResult.ReadOnly = true;
			txtResult.MouseClick += new MouseEventHandler(txtResult_MouseClick);

			this.EndUpdate();
		}

		#endregion

		#region Public Members

		#region Properties
		public override ResizeSides ResizableSides
		{
			get
			{
				return ResizeSides.Left | ResizeSides.Bottom;
			}
		}
		public override string ResultString
		{
			get
			{
				string parts = "";
				foreach ( IParticipant part in _participants ) {
					string s = null;
					if ( part is User ) {
						s = string.Format("u:{0}:{1}", part.ID, part.Display);
					} else if ( part is Role ) {
						s = string.Format("r:{0}:{1}", part.ID, part.Display);
					} else if ( part is OrganizationalUnit ) {
						s = string.Format("ou:{0}:{1}", part.ID, part.Display);
					} else if ( part is Organization ) {
						s = string.Format("o:{0}:{1}", part.ID, part.Display);
					}
					if ( s != null ) {
						if ( parts != "" ) parts += ";";
						parts += s;
					}
				}
				return parts;
			}
			set
			{
				string[] parts = value.Split(';');
				if ( _participants == null )
					_participants = new Participants();
				else
					_participants.Clear();
				foreach ( string part in parts ) {
					string[] typeidname = part.Split(':');
					if ( typeidname.Length != 3 )
						continue;
					try {
						int id;
						if ( int.TryParse(typeidname[1], out id) ) {
							if ( typeidname[0] == "u" ) {
								_participants.Add(Global.Repository.GetItem<User>(id));
							} else if ( typeidname[0] == "r" ) {
								_participants.Add(Global.Repository.GetItem<Role>(id));
							} else if ( typeidname[0] == "ou" ) {
								_participants.Add(Global.Repository.GetItem<OrganizationalUnit>(id));
							} else if ( typeidname[0] == "o" ) {
								_participants.Add(Global.Repository.GetItem<Organization>(id));
							}
						}
					} catch {
					}
				}
				updateInfo();
			}
		}
		#endregion

		#region Methods
		public override void Paint(PaintEventArgs pe)
		{
			base.Paint(pe);
			Rectangle rc = this.GetPaintClientArea();
			pe.Graphics.DrawImageUnscaled(Resources.png_user, rc.Right - cImgWH, rc.Top);
		}
		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected override void OnMouseClick(MouseEventArgs e)
		{
			base.OnMouseClick(e);
			Rectangle rc = this.GetPaintClientArea();
			rc = new Rectangle(rc.Right - cImgWH, rc.Top, cImgWH, cImgWH);
			if ( rc.Contains(e.Location) ) {
				this.DesignerViewControl.BeginInvoke(new MethodInvoker(showPartSelectForm));
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private const int cImgWH = 16;
		private Participants _participants;
		#endregion

		#region Methods
		private void showPartSelectForm()
		{
			FormParticipants frm = new FormParticipants();
			frm.Init(_participants);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				updateInfo();
			}
		}
		private void updateInfo()
		{
			string info = "";
			foreach ( IParticipant part in _participants ) {
				if ( info != "" ) info += "; ";
				info += part.Display;
			}
			txtResult.Text = info;
		}
		#endregion

		#region Event Handlers
		private void txtResult_MouseClick(object sender, MouseEventArgs e)
		{
			this.DesignerViewControl.BeginInvoke(new MethodInvoker(showPartSelectForm));
		}
		#endregion

		#endregion

		#region IXmlSerializable Members

		public override XmlSchema GetSchema()
		{
			return null;
		}

		public override void ReadXml(XmlReader reader)
		{
			while ( reader.Read() ) {
				switch ( reader.Name ) {
					case "DesignerControl":
						if ( reader.NodeType == XmlNodeType.EndElement )
							return;
						break;
					case "PARTICIPANTS":
						this.ResultString = reader.ReadString();
						break;
					default:
						ReadStandardProperties(reader);
						break;
				}
			}
		}

		public override void WriteXml(XmlWriter writer)
		{
			WriteStandardProperties(writer);

			// Start PARTICIPANTS
			writer.WriteStartElement("PARTICIPANTS");
			writer.WriteString(this.ResultString);
			writer.WriteEndElement();
			// End PARTICIPANTS
		}

		#endregion
	}
}
